package top.xiaoshuaila.searescue.mqtt;

import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Component
public class EmqClient {
    private IMqttClient mqttClient;
    @Autowired
    private MqttProperties mqttProperties;
    @Autowired
    private MqttCallback mqttCallback;


    /*构造函数执行后执行2*/
    @PostConstruct
    public void init(){
        //持久化机制,储存到内存
        MqttClientPersistence mqttClientPersistence=new MemoryPersistence();
        try {
            //创建一个mqtt客户端
            mqttClient=new MqttClient(mqttProperties.getUrl(),mqttProperties.getId(),mqttClientPersistence);
            System.out.println("创建客户端成功");
        } catch (MqttException e) {
            System.out.println("创建客户端失败"+e.getMessage());
        }
    }
    /*连接*/
    public void connect(String username,String password){
        MqttConnectOptions options=new MqttConnectOptions();
        //设置自动重连
        options.setAutomaticReconnect(true);
        options.setUserName(username);
        options.setPassword(password.toCharArray());
        options.setPassword(password.toCharArray());
        //离线消息,临时会话
        options.setCleanSession(true);
        //方法回调,
        mqttClient.setCallback(mqttCallback);
        try {
            mqttClient.connect(options);
            System.out.println("连接成功");
        } catch (MqttException e) {
            System.out.println("连接失败"+e.getMessage());
        }
    }
    /*在对象销毁前断开连接*/
    @PreDestroy
    public void disconnect(){
        try {
            mqttClient.disconnect();
            System.out.println("打开链接成功");
        } catch (MqttException e) {
            System.out.println("断开链接失败"+e.getMessage());
        }
    }
    /*重新连接*/
    public void reconnect(){
        try {
            mqttClient.reconnect();
            System.out.println("重连成功");
        } catch (MqttException e) {
            System.out.println("重连失败"+e.getMessage());
        }
    }
    /*向topic发布消息*/
    public void publish(String topic,String msg,int qos,boolean retain){
        MqttMessage mqttMessage=new MqttMessage();
        mqttMessage.setPayload(msg.getBytes());
        mqttMessage.setQos(qos);
        mqttMessage.setRetained(retain);
        try {
            mqttClient.publish(topic,mqttMessage);
            System.out.println("发布消息成功");
        } catch (MqttException e) {
            System.out.println("发布消息失败"+e.getMessage());
        }
    }
    /*向topic发布16进制消息*/
    public void publish(String topic,byte[] msg,int qos,boolean retain){
        MqttMessage mqttMessage=new MqttMessage();
        mqttMessage.setPayload(msg);
        mqttMessage.setQos(qos);
        mqttMessage.setRetained(retain);
        try {
            mqttClient.publish(topic,mqttMessage);
            System.out.println("发布消息成功");
        } catch (MqttException e) {
            System.out.println("发布消息失败"+e.getMessage());
        }
    }
    /*订阅某个主题*/
    public void substribe(String topicFilters,int qos){
        try {
            mqttClient.subscribe(topicFilters,qos);
            System.out.println("订阅主题成功");
        } catch (MqttException e) {
            System.out.println("订阅主题失败");
        }
    }
    /*取消订阅某个主题*/
    public void unsubstribe(String topicFilters){
        try {
            mqttClient.unsubscribe(topicFilters);
            System.out.println("取消订阅成功");
        } catch (MqttException e) {
            System.out.println("取消订阅失败");
        }
    }
}
